package com.wushi.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;

public class MYFilerManager {

	public static void main(String[] args) {
		//fileList("/Users/heitu");

		//copyBinaryFile("/Users/heitu/Downloads/ideaIU-2018.1.4.dmg", "/Users/heitu/test04bak.dmg");
		
		copyCharFile("/Users/heitu/PULL_REQUEST_TEMPLATE.md","/Users/heitu/test.txt");
	}

	public static void fileList(String filename) {
		File file = new File(filename);
		Stream<File> stream = Arrays.stream(file.listFiles());
		stream.filter(fileitem -> fileitem.isFile()).forEach(fileitem -> System.out.println(fileitem.getName()));

	}
	
	public static void copyCharFile(String oldname, String newname){
		File oldfile = new File(oldname); // 源文件
		File newfile = new File(newname); // 新文件
		if (!oldfile.exists()) {
			System.out.println("#########3old file notextisx");
			return;
		}
		if (!newfile.exists())
			try {
				newfile.createNewFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		try {
			FileReader reader=new FileReader(oldfile);
			FileWriter writer=new FileWriter(newfile);
			BufferedReader breader=new BufferedReader(reader);
			BufferedWriter bwriter=new BufferedWriter(writer);
			
			char[] cbuf=new char[1024];
			int len=0;
			while((len=breader.read(cbuf))!=-1) {
				bwriter.write(cbuf, 0, len);
			}
			bwriter.flush();
			reader.close();
			writer.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/*
	 * 采用字节流的方式copy一份新文件
	 */
	public static void copyBinaryFile(String oldname, String newname) {
		File oldfile = new File(oldname); // 源文件
		File newfile = new File(newname); // 新文件
		if (!oldfile.exists()) {
			System.out.println("#########3old file notextisx");
			return;
		}
		if (!newfile.exists())
			try {
				newfile.createNewFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		int len = 0;
		byte[] b = new byte[1024];
		// 二进制流
		try {
			FileInputStream inputStream = new FileInputStream(oldfile);
			FileOutputStream outputStream = new FileOutputStream(newfile);
			BufferedInputStream bufferin = new BufferedInputStream(inputStream);  //采用bufferstream比直接stream提高5倍左右速度
			BufferedOutputStream bufferout = new BufferedOutputStream(outputStream);
			long start = System.currentTimeMillis();
			System.out.println("#####start" + start);
			while ((len = bufferin.read(b)) != -1) {
				// len=inputStream.read(b);
				// System.out.println(new String(b));
				// outputStream.write(b, 0, len);
				bufferout.write(b, 0, len);
			}
			bufferout.flush();
			long end = System.currentTimeMillis();
			System.out.println("#####end" + end);
			System.out.println("#####duringtime" + (end - start));

			inputStream.close();
			outputStream.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			System.out.println("##############finished");
		}

	}

}
